from utils.querys import queryhive
from utils.getPublicData import *
from myApp.models import *

def getIndexData():
    movieTypeCountList = list(getmovieTypeCount())
    print(movieTypeCountList[0])
    typeChartData = []
    for i in movieTypeCountList:
        typeChartData.append({
            'name':i[0],
            'value':i[1]
        })
    boxTopMovieList = list(getboxTopMovie())
    typeMaxBoxList = list(gettypeMaxBox())
    mcountryCountList = list(getmcountryCount())
    starCategoryList = list(getstarCategory())
    # boxTopMovieList[0],
    print(boxTopMovieList[0])
    # print(typeMaxBoxList[0],mcountryCountList[0],starCategoryList[0])
    topX = [x[0] for x in boxTopMovieList]
    topY1 = [x[11] for x in boxTopMovieList]
    topY2 = [x[6]/10 for x in boxTopMovieList]
    # print(topX,topY1,topY2)
    boxLineX = [x[0] for x in typeMaxBoxList][:10]
    boxLineY1 = [x[1] for x in typeMaxBoxList][:10]
    boxLineY2 = [x[2] for x in typeMaxBoxList][:10]

    countryX = [x[0] for x in mcountryCountList][:10]
    countryY = [x[1] for x in mcountryCountList][:10]

    starData = []
    for i in starCategoryList:
        starData.append({
            'name':i[0],
            'value':i[1]
        })
    total_value = sum(item['value'] for item in starData)

    for item in starData:
        item['reaVal'] = round(item['value'] / total_value * 100,2)
    # print(starData)
    return typeChartData,topX,topY1,topY2,boxLineX,boxLineY1,boxLineY2,countryX,countryY,starData,boxTopMovieList


def getTypeChartData():
    perTypeBoxList = list(getperTypeBox())
    typeList = list(set([x[0] for x in perTypeBoxList]))
    print(typeList)
    typeAvgTimeList = list(gettypeAvgTime())
    typeAvgData = []
    for i in typeAvgTimeList:
        typeAvgData.append({
            'name':i[0],
            'value':round(i[1],2)
        })
    print(typeAvgData)
    return typeList,typeAvgData[:10]


def getChartOneByType(defaultType):
    perTypeBoxList = list(getperTypeBox())
    perTypeRateList = list(getperTypeRate())
    print(perTypeBoxList[0],perTypeRateList[0])
    boxDataX = []
    boxDataY = []
    rateDataX = []
    rateDataY = []
    for i in perTypeBoxList:
        if i[0] == defaultType:
            boxDataX.append(i[1])
            boxDataY.append(i[2])

    for i in perTypeRateList:
        if i[0] == defaultType:
            rateDataX.append(i[1]/10)
            rateDataY.append(i[2])

    print(boxDataX,boxDataY,rateDataX,rateDataY)
    return boxDataX[::-1],boxDataY[::-1],rateDataX,rateDataY


def getRateChartData():
    mYearRateList = list(getmYearRate())
    mCountryRateList = list(getmCountryRate())
    mTypeRateList = list(getmmTypeRate())
    print(mYearRateList[0],mCountryRateList[0],mTypeRateList[0])
    mYearRateList = [x for x in mYearRateList if x[0] is not None]
    mYearX = [x[0] for x in mYearRateList]
    mYearY = [round(x[1]/10,2) for x in mYearRateList]
    print(mYearX,mYearY)

    mCountryX = [x[0] for x in mCountryRateList ]
    mCountryY = [round(x[1]/10,2) for x in mCountryRateList]
    print(mCountryX,mCountryY)

    mTypeX = [x[0] for x in mTypeRateList]
    mTypeY = [round(x[1]/10,2) for x in mTypeRateList]
    print(mTypeX,mTypeY)
    print(len(mYearX),len(mCountryX),len(mTypeX))
    return mYearX,mYearY,mCountryX,mCountryY,mTypeX,mTypeY


def getTimeChartData():
    mYearCountList = list(getmmYearCount())
    mMonthCountList =list(getmMonthCount())
    mTimeCategoryList = list(getmTimeCategory())
    print(mYearCountList[0],mMonthCountList[0],mTimeCategoryList[0])
    mYearCountList = [x for x in mYearCountList if x[0] is not None]
    mMonthCountList = [x for x in mMonthCountList if x[0] is not None]
    mMonthCountList = list(sorted(mMonthCountList,key=lambda item:item[0]))

    mYearX = [x[0] for x in mYearCountList]
    mYearY = [x[1] for x in mYearCountList]
    mMonthX = [x[0] for x in mMonthCountList]
    mMonthY = [x[1] for x in mMonthCountList]

    durationData = []
    for i in mTimeCategoryList:
        durationData.append({
            'name':i[0],
            'value':i[1]
        })

    # print(mYearX,mYearY,mMonthX,mMonthY,durationData)
    return mYearX,mYearY,mMonthX,mMonthY,durationData


def getBoxChartData():
    mTypeAvgBoxList = list(getmTypeAvgBox())
    mCountryAvgBox = list(getmCountryAvgBox())
    mYearAvgBoxList = list(getmYearAvgBox())
    print(mTypeAvgBoxList[0],mCountryAvgBox[0],mYearAvgBoxList[0])

    mTypeBoxX = [x[0] for x in mTypeAvgBoxList]
    mTypeBoxY = [round(x[1],2)for x in mTypeAvgBoxList]

    mCountryAvgBox = [x for x in mCountryAvgBox if x[1] is not None]
    mYearAvgBoxList = [x for x in mYearAvgBoxList if x[0] is not None]

    mCountryBoxX = [x[0] for x in mCountryAvgBox]
    mCountryBoxY = [x[1] for x in mCountryAvgBox]
    print(mCountryBoxY)

    mYearX = [x[0] for x in mYearAvgBoxList]
    mYearY = [round(x[1],2)for x in mYearAvgBoxList]

    return mTypeBoxX,mTypeBoxY,mCountryBoxX,mCountryBoxY,mYearX,mYearY


def addHistoryData(userInfo,movieId):
    hisData = History.objects.filter(user=userInfo,movieId=movieId)
    if len(hisData):
        hisData[0].count += 1
        hisData[0].save()
    else:
        History.objects.create(user=userInfo,movieId=movieId)



def getUserHistory(userInfo):
    dataList = list(History.objects.filter(user=userInfo).order_by('-count'))
    movieIdList = []
    for i in dataList:
        movieIdList.append(i.movieId)

    movieList = []
    for i in movieIdList:
        x = list(queryhive('select * from catMovieData where id = %s',[int(i)],'select'))
        # print(x)
        movieList.append(x[0])

    # print(movieList[0])
    return movieList


def changePwd(userInfo,passwordInfo):
    oldPwd = passwordInfo['oldPwd']
    newPwd = passwordInfo['newPwd']
    ckPwd = passwordInfo['ckPwd']

    user = User.objects.get(username=userInfo.username)

    if oldPwd != user.password : return '原密码错误'
    if newPwd != ckPwd : return '请再次确认您的密码'
    user.password = newPwd
    user.save()